package cn.shutdown.demo.thread.deadlock.youhua01;

/**
 * @author jiangwujie
 * @date 2021/9/1
 */
public class Test01 {

    public static void main(String[] args) throws InterruptedException {
        Obj obj = new Obj();
        ThreadA threadA1 = new ThreadA(obj);
        threadA1.setName("threadA1");
        ThreadA threadA2 = new ThreadA(obj);
        threadA2.setName("threadA2");
        ThreadB threadB = new ThreadB(obj);
        threadB.setName("threadB");


        Thread.sleep(1000);
        threadA1.start();
        threadA2.start();
        threadB.start();
    }

}

class ThreadA extends Thread {

    private Obj obj;

    public void run() {
        synchronized (obj) {
            obj.A();
        }
    }

    public ThreadA(Obj obj) {
        this.obj = obj;
    }

}

class ThreadB extends Thread {

    private Obj obj;

    public void run() {
        obj.B();
        obj.A();
    }

    public ThreadB(Obj obj) {
        this.obj = obj;
    }
}

class Obj {
    public void A() {
        System.out.println(Thread.currentThread().getName() + "A runs");
    }

    public void B() {
        System.out.println(Thread.currentThread().getName() + "B runs");
    }
}
